home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 214 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  85 lines

  1. Path: lrz-muenchen.de!sun2!ua302aa
  2. From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Array Parameters
  5. Date: 3 Jan 1996 11:12:13 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4cdoad$4v2@sparcserver.lrz-muenchen.de>
  9. References: <wayne.820650643@hawk>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. wayne@adied.oz.au (Wayne Elliott) writes:
  13.  
  14. >Can anyone fill me in on whats wrong with the following snippet of
  15. >code. Basically I'm trying to pass and use a multi-dimensional
  16. >array.
  17.  
  18. >-----------------------------------------------------------------
  19.  
  20. >/* Test passing of array parameters */
  21.  
  22. >#include <stdio.h>
  23.  
  24. >char map [][8] =
  25. >{
  26. >  "abcdef",
  27. >  "ghijkl",
  28. >  "mnopqr"
  29. >};
  30.  
  31. Please note that map is an array of unknown size of arrays of 8 chars
  32.  
  33. >void test(int num, char ** maps)
  34. >{
  35. >  int i;
  36. >  for(i=0;i<num;i++)
  37. >    if(maps[i])
  38. >      printf("%04d %s\n", i, maps[i]);
  39. >    else
  40. >      printf("%04d NULL\n", i);
  41. >}
  42.  
  43. >int main()
  44. >{
  45. >  test(3, (char **) map);
  46.  
  47. Why do you cast map to a char**? Maybe becuase you got a diagnostic
  48. message without the cast? The diagnostic message was helpful, but
  49. you decided to make it go away.
  50.  
  51. You have at least two possible ways to get a working program:
  52.  
  53. 1) Define map as an array of pointers to char:
  54.  
  55.   char *map[] = { /* Initializers */ };
  56.  
  57. 2) Change the definition of test so that it accepts a pointer to 
  58.    arrays of 8 chars:
  59.  
  60.    void test(int num, char (* map)[8]);
  61.  
  62. >  return 0;
  63. >}
  64.  
  65. >-----------------------------------------------------------------
  66.  
  67. >At home this only finds the first element (map[0]).
  68. >At work this gives a core dump.
  69. >Using pointer arithmetic (maps+i) produced something like
  70.  
  71. >0000 abcedf
  72. >0001 ef
  73. >0002 ghijkl
  74.  
  75. >It's all rather comfusing!
  76. >Any clues?  How should I be doing this?
  77.  
  78. Don't confuse pointers and arrays!
  79.  
  80. Kurt
  81. --
  82. | Kurt Watzka                             Phone : +49-89-2180-6254
  83. | watzka@stat.uni-muenchen.de
  84. | ua302aa@sunmail.lrz-muenchen.de
  85.